package com.zretc.controller;

import com.aliyuncs.exceptions.ClientException;
import com.zretc.entity.Seller;
import com.zretc.entity.Type;
import com.zretc.service.SellerService;
import com.zretc.util.AliSMSUtil;
import com.zretc.util.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.RequestScope;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@CrossOrigin
@RequestMapping("/seller")
public class SellerController {

    private static final Logger logger = LoggerFactory.getLogger(SellerController.class);

    @Autowired
    private SellerService sellerService;

    @RequestMapping("/login")
    @ResponseBody
    public ResponseEntity<Seller> login(@RequestParam(value = "sellerAccount") String sellerAccount,
                                        @RequestParam(value = "password") String password) {
        String sellerPassword = Util.getMd5(password);
        logger.info("====登录用户名：" + sellerAccount);
        logger.info("====登录密码：" + sellerPassword);
        Seller seller = sellerService.login(sellerAccount, sellerPassword);
        logger.info("========" + seller);
        if (seller == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(seller);
    }

    @RequestMapping("/register")//注册
    @ResponseBody
    public boolean register(@RequestParam(value = "sellerAccount") String sellerAccount,//账号
                            @RequestParam(value = "password") String password,//密码
                            @RequestParam(value = "sellerAddress") String sellerAddress,//地址
                            @RequestParam(value = "sellerPhone") String sellerPhone,//手机号码
                            @RequestParam(value = "sellerName") String sellerName,//店铺名字
                            @RequestParam(value = "logo") MultipartFile logo//店铺logo
    ) {
        Integer sellerStatus = 0;
        String sellerPassword = Util.getMd5(password);//加密
        logger.info("======" + password);
        String sellerLogo = Util.getLogo(logo);//保存图片到指定位置
        Seller seller = new Seller(null, sellerAccount, sellerPassword, sellerAddress,
                sellerPhone, sellerName, sellerLogo, sellerStatus);
        int t = sellerService.register(seller);
        if (t == 0) {
            return false;
        }
        return true;
    }

    @RequestMapping("/update")
    @ResponseBody

    public ResponseEntity<Seller> update(@RequestParam(value = "sellerAccount") String sellerAccount,//账号
                                         String password,//密码
                                         String sellerAddress,//地址
                                         String sellerPhone,//手机号码
                                         String sellerName,//店铺名字
                                         MultipartFile logo//店铺logo
    ) {

        Map<String, Object> param = new HashMap<>();
        Boolean flag = false;
        if (!"".equals(sellerAccount) && sellerAccount != null) {
            param.put("sellerAccount", sellerAccount);
        }
        if ("".equals(password) || password == null) {
            param.put("sellerPassword", null);
        } else {
            param.put("sellerPassword", Util.getMd5(password));//加密
            flag = true;
        }
        if ("".equals(sellerAddress) || sellerAddress == null) {
            param.put("sellerAddress", null);
        } else {
            param.put("sellerAddress", sellerAddress);
            flag = true;
        }
        if ("".equals(sellerPhone) || sellerPhone == null) {
            param.put("sellerPhone", null);
        } else {
            param.put("sellerPhone", sellerPhone);
            flag = true;
        }
        if ("".equals(sellerName) || sellerName == null) {
            param.put("sellerName", null);
        } else {
            param.put("sellerName", sellerName);
            flag = true;
        }

        String sellerLogo = Util.getLogo(logo);//保存图片到指定位置
        logger.debug(sellerLogo+"===");
        if (sellerLogo != null) {
            flag = true;
            param.put("sellerLogo", sellerLogo);
        }
        logger.debug(param.toString());
        if (flag) {
            Seller seller = sellerService.update(param);
            if (seller == null) {
                new ResponseEntity(null, HttpStatus.NO_CONTENT);
            } else {
                return ResponseEntity.ok(seller);
            }

        }
        return new ResponseEntity(null, HttpStatus.NO_CONTENT);

    }

    @RequestMapping("/getPhone")
    @ResponseBody
    public boolean getPhone(@RequestParam(value = "sellerPhone") String sellerPhone, HttpSession session) {
        String s = "";
        try {
            s = AliSMSUtil.sendCode(sellerPhone);
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
        session.setAttribute("fastLoginCode", s);
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                session.removeAttribute("fastLoginCode");
                timer.cancel();
            }
        }, 5 * 60 * 1000);
        return true;
    }


}
